<div class="layui-card">
    <div class="layui-card-header">
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>服务管理</cite></a>
        </span>
    </div>

    <div class="layui-card-body">
        <div class="layui-form toolbar nowrap">
            <button id="btn-hall" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>重读大厅数据库</button>
            <button id="btn-game" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>重读游戏数据库</button>
        </div>
        <table class="layui-table" id="server-table" lay-filter="server-table">

        </table>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="server-bar">
    <!--href="pages/websocket/index.html"-->
    <a class="layui-btn layui-btn-primary layui-btn-xs"  lay-event="open">打开控制台</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="close">关闭服务程序</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="start">启动服务程序</a>
</script>

<script>


    layui.use(['form', 'table', 'util', 'config', 'admin','laydate','layer'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var laydate = layui.laydate;
        var layer = layui.layer;

        table.render({
            elem: '#server-table',
            url: config.base_server + 'api-operation/server/getServerInfo',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {field: 'serverName',width:150, title: '服务器名称'},
                {field: 'ip',width:200, title: '服务器IP'},
                {fixed: 'right',width:400, align:'center', toolbar: '#server-bar'}
            ]]
        });
        //监听工具条:lay-event对应值
        table.on('tool(server-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'open') {
                openServer(data);
            }else if (obj.event === 'close') {
                closeServer(data);
            }else if (obj.event === 'start') {
                startServer(data);
            }
        });

        var closeServer = function (data) {
            $.ajax({
                type : 'PUT',
                url : config.base_server + 'api-operation/server/closeServer',
                data : {access_token : config.getToken().access_token,
                    ip : data.ip} ,
                error : function(data) {
                    alert(data.resp_msg);
                },
                success : function(data) {
                    alert(data.resp_msg);
                }
            });
            table.reload('server-table');
        };

        var startServer = function (data) {
            $.ajax({
                type : 'PUT',
                url : config.base_server + 'api-operation/server/startServer',
                data : {access_token : config.getToken().access_token,
                    ip : data.ip} ,
                error : function(data) {
                    alert(data.resp_msg);
                },
                success : function(data) {
                    alert(data.resp_msg);
                }
            });
            table.reload('server-table');
        };

        var openServer = function (data) {
            var serverId = data.ip;
            admin.req('api-operation/server/getCode', {}, function (data) {
                layer.prompt(function(val, index){
                    $.ajax({
                        type : 'PUT',
                        url : config.base_server + 'api-operation/server/verifyCode',
                        data : {access_token : config.getToken().access_token,
                            val :val} ,
                        success : function(data) {
                            console.log(data)
                            layer.msg(data.resp_msg);
                            if (data.resp_code == 0){
                                var index = layer.open({
                                    type: 2,
                                    content: 'pages/websocket/index.html/?serverId='+serverId ,
                                    area: ['100%', '100%'],
                                    maxmin: true
                                });
                            }
                        }
                    });
                });
        }, 'PUT');
        };
    });
</script>
